#include<bits/stdc++.h>
using namespace std;

int main() {
	int n;
	cin >> n;
	vector<int>head(2 * n + 2, 0), tail = head;
	vector<vector<long long> >dp(2 * n + 2, 
		vector<long long>(2 * n + 2, 0));
	for (int i = 1; i <= n; i++) {
		cin >> head[i];
		head[i + n] = head[i];
	}
	for (int i = 1; i <= 2 * n; i++) {
		int t = i + 1;
		if (t > 2 * n)
			t = 1;
		tail[i] = head[t];
	}
	for(int len=1,j;len<=n;len++)
		for (int i = 1; i + len - 1 <= 2*n; i++) {
			j = i + len - 1;
			for (int k = i; k < j; k++)
				dp[i][j] = max(dp[i][j], 
					dp[i][k] + dp[k + 1][j] + head[i] * tail[k] * tail[j]);
		}
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		ans = max(ans, dp[i][i + n - 1]);
	cout << ans;
	return 0;
}